//
//  MainTabBar.swift
//  WbHenryTest
//
//  Created by 张洪毓 on 15/11/11.
//  Copyright © 2015年 张洪毓. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

    //增加 tabbar 中间的 + 按钮
     lazy var composeButton: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
//        button.sizeToFit()
        button.layer.cornerRadius = 28
//        button.layer.masksToBounds = true
        button.clipsToBounds = true
        return button
    }()
    
    //手动布局 tabbar 子控件的frame
    override func layoutSubviews() {
        super.layoutSubviews()
        let w = self.bounds.width / 5
        let h = self.bounds.height
//        let rect = CGRectMake(0, 0, w, h)
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        
        //定义递增变量
        var index: CGFloat = 0
        for subView in subviews {
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!){
                subView.frame = CGRectOffset(rect, w * index, 0)
                index += index == 1 ? 2 : 1
            }
        }
        //设置 + 按钮的frame
        composeButton.frame = CGRectOffset(rect, w * 2, 0)
        bringSubviewToFront(composeButton)
    }
    //重写默认的构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    //如果重写 init(frame:) 方法，那么系统会默认为这个控件是通过代码创建
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    private func setupUI() {
        addSubview(composeButton)
    }
    
    
}
